﻿using MY.ShoppingBackstage.BLL;
using MY.ShoppingBackstage.Model;
using MY.ShoppingBackstage.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MY.ShoppingBackstage.Controllers
{

    /// <summary>
    /// 商品管理控制器
    /// </summary>
    public class CommodityController : Controller
    {
        private BrandService brandService = new BrandService();

        private CategoryService categoryService = new CategoryService();

        private Product_SpuService product_SpuService = new Product_SpuService();
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <returns></returns>
        public ActionResult Add_Product(Nullable<int> id)
        {
            ViewBag.brand = brandService.QueryList(a=>true);
            ViewBag.category = categoryService.QueryList(a=>true);
            ViewBag.product = product_SpuService.Query(a=>a.Id==id)??new Product_Spu();            
            ViewBag.specs = product_SpuService.GetSpecs(id??0)??new List<Specs>();
            return View();
        }

        public ActionResult ClassManager()
        {
            return View();
        }
        /// <summary>
        /// 品牌管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Brand_Manage()
        {
            return View();
        }
        /// <summary>
        /// 品牌管理下的添加品牌功能页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Add_Brand()
        {
            return View();
        }
        /// <summary>
        /// 分类页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Products()
        {
            return View();
        }
        public ActionResult Add_Class(string id)
        {
            Category category = new Category();
            if (!string.IsNullOrEmpty(id))
            {
                int _id = Convert.ToInt32(id);
                category = categoryService.Query(b => b.Id == _id);
            }
            ViewBag.category = category;
            //获取父级
            ViewBag.Parent = categoryService.QueryList(c => c.CategoryLevel != 3);
            return View();
        }

        #region 品牌
        /// <summary>
        /// 得到所有品牌信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetBrands(int page, int limit)
        {
            PageListResult<Brand> result = new PageListResult<Brand>()
            {
                data = brandService.QueryPageList<int>(page, limit, a => true, a => a.Id)
            };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public JsonResult DelBrand(int Id)
        {
            OperateResult operate = new OperateResult()
            {
                Success = brandService.Delete(Id)
            };
            return Json(operate);
        }

        /// <summary>
        /// 修改品牌状态
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public JsonResult UpdateStatusByBrand(int Id, short Status)
        {
            OperateResult operate = new OperateResult()
            {
                Success = brandService.UpdateStatusByBrand(Id, Status)
            };
            return Json(operate);
        }
        /// <summary>
        /// 新增品牌
        /// </summary>
        /// <param name="brand"></param>
        /// <returns></returns>
        public JsonResult AddBrand(Brand brand)
        {
            brand.UpdateTime = DateTime.Now;
            OperateResult operate = new OperateResult()
            {
                Success = brandService.Add(brand)
            };
            return Json(operate);
        }
        /// <summary>
        /// 修改品牌
        /// </summary>
        /// <param name="brand"></param>
        /// <returns></returns>
        public JsonResult UpdateBrand(Brand brand)
        {
            brand.UpdateTime = DateTime.Now;
            OperateResult operate = new OperateResult()
            {
                Success = brandService.Update(brand)
            };
            return Json(operate);
        }
        #endregion

        #region 分类操作
        /// <summary>
        /// 得到所有分类
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetClasses(int page, int limit, string name, string datetime, string end)
        {
            int count = 0;

            List<Category> list = new List<Category>();
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(datetime))
            {
                DateTime date = DateTime.Parse(datetime);
                list = categoryService.QueryPageList(page, limit, b => b.Name.Contains(name) && b.ModifiedTime >= date && b.ModifiedTime <= DateTime.Now, b => b.Id);
                count = categoryService.GetCount(b => b.Name.Contains(name) && b.ModifiedTime >= date && b.ModifiedTime <= DateTime.Now, out count);
            }
            else if (!string.IsNullOrEmpty(datetime) && !string.IsNullOrEmpty(end))
            {
                DateTime date = DateTime.Parse(datetime);
                DateTime _end = DateTime.Parse(end);
                list = categoryService.QueryPageList(page, limit, b => b.ModifiedTime >= date && b.ModifiedTime <= _end, b => b.Id);
                count = categoryService.GetCount(b => b.ModifiedTime >= date && b.ModifiedTime <= _end, out count);
            }
            else if (!string.IsNullOrEmpty(datetime))
            {
                DateTime date = DateTime.Parse(datetime);
                list = categoryService.QueryPageList(page, limit, b => b.ModifiedTime >= date && b.ModifiedTime <= DateTime.Now, b => b.Id);
                count = categoryService.GetCount(b => b.ModifiedTime >= date && b.ModifiedTime <= DateTime.Now, out count);
            }
            else if (!string.IsNullOrEmpty(name))
            {
                list = categoryService.QueryPageList(page, limit, b => b.Name.Contains(name), b => b.Id);
                count = categoryService.GetCount(b => b.Name.Contains(name), out count);
            }
            else
            {
                list = categoryService.QueryPageList(page, limit, b => b.Id != 0, b => b.Id);
                count = categoryService.GetCount(a => true, out count);
            }
            PageListResult<Category> plr = new PageListResult<Category>();
            plr.code = 0;
            plr.msg = "";
            plr.data = list;
            plr.count = count;

            return Json(plr, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public JsonResult DelClass(int Id)
        {
            OperateResult operate = new OperateResult()
            {
                Success = categoryService.Delete(Id)
            };
            return Json(operate);
        }
        /// <summary>
        /// 添加或者修改分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public JsonResult AddOrUpdateClass(Category category)
        {
            category.ModifiedTime = DateTime.Now;
            OperateResult operate = new OperateResult();
            if (category.Id == 0)
            {
                //添加
                operate.Success = categoryService.Add(category);
                operate.Info = operate.Success ? "添加成功" : "添加失败";
            }
            else
            {
                //修改
                operate.Success = categoryService.Update(category);
                operate.Info = operate.Success ? "修改成功" : "修改失败";
            }
            return Json(operate);
        } 
        #endregion

        /// <summary>
        /// 得到产品信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetProducts(int page, int limit)
        {
            List<Product_SpuView> product_s = product_SpuService.GetProducts();
            PageListResult<Product_SpuView> pageList = new PageListResult<Product_SpuView>() {
                data = product_s,
                count= product_s.Count
            };
            return Json(pageList,JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 修改商品上下架
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult UpdatePicductStatus(int Id,short Status)
        {
            Product_Spu product = product_SpuService.Query(a=>a.Id==Id);
            product.Publish_Status = Status;

            OperateResult operate = new OperateResult()
            {
                Success=product_SpuService.Update(product)
            };
            return Json(operate);
        }        

        /// <summary>
        /// 新增产品
        /// </summary>
        /// <param name="product_Spu"></param>
        /// <param name="Sku"></param>
        /// <returns></returns>
        public JsonResult AddProduct(Product_Spu product_Spu,string Specs)
        {
            List<Specs> list = JsonConvert.DeserializeObject<List<Specs>>(Specs);

            OperateResult operate = new OperateResult() {
                Success=product_SpuService.AddProduct(product_Spu,list)
            };
            return Json(operate);
        }

        /// <summary>
        /// 根据id返回图片路径
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult ShowImg(int id)
        {
            OperateResult operate=new OperateResult() {
                Info = brandService.Query(b => b.Id == id).Logo
            };
            return Json(operate);
        }
    }
}